import
CONFIG
from '../config/config.js'

/**
 * 设置上传图片数组路径
 * @param {Object} fileList
 * @param {Object} uploadList
 */
export function setUploadUrlData(uploadList) {
	//uploadList:上传的文件列表
	const fileList = [];
	console.log(uploadList)
	uploadList.forEach(val => {
		//筛选进度为100的图片
		if (val.progress == 100) {
			val.response ? fileList.push(val.response.data.url) :
				fileList.push(val.url.replace(CONFIG.baseUrl, ''));
		}
	})
	return fileList;
}

export function dateFormats(timestamp, formats) {
    // formats格式包括
    // 1. Y-m-d
    // 2. Y-m-d H:i:s
    // 3. Y年m月d日
    // 4. Y年m月d日 H时i分
	console.log(timestamp)
	console.log(formats)
    formats = formats || 'Y-m-d';

    var zero = function (value) {
        if (value < 10) {
            return '0' + value;
        }
        return value;
    };

    var myDate = timestamp ? new Date(timestamp) : new Date();

    var year = myDate.getFullYear();
    var month = zero(myDate.getMonth() + 1);
    var day = zero(myDate.getDate());

    var hour = zero(myDate.getHours());
    var minite = zero(myDate.getMinutes());
    var second = zero(myDate.getSeconds());

    return formats.replace(/Y|m|d|H|i|s/ig, function (matches) {
        return ({
            Y: year, m: month, d: day, H: hour, i: minite,
        })[matches];
    });
}

//日期格式化
export function dateFormat(fmt, date) {
	let ret;
	const opt = {
		"Y+": date.getFullYear().toString(), // 年
		"m+": (date.getMonth() + 1).toString(), // 月
		"d+": date.getDate().toString(), // 日
		"H+": date.getHours().toString(), // 时
		"M+": date.getMinutes().toString(), // 分
		"S+": date.getSeconds().toString() // 秒
		// 有其他格式化字符需求可以继续添加，必须转化成字符串
	};
	for (let k in opt) {
		ret = new RegExp("(" + k + ")").exec(fmt);
		if (ret) {
			fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
		};
	};
	return fmt;
}

//邮箱验证
export function isEmail(str) {
	var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
	return reg.test(str);
}

// 定时器方案
export function throttle(fn,wait){
    var timer = null;
    return function(){
        var context = this;
        var args = arguments;
        if(!timer){
            timer = setTimeout(function(){
                fn.apply(context,args);
                timer = null;
            },wait)
        }
    }
}
//设置图片路径
export function setImgPath (str) {
	const arr = str.split(',');
	arr.forEach((item, index) => {
		arr[index] = CONFIG.baseUrl+ item;
	})
	return arr;
}
